# 206. 反转链表
# 简单
# 给你单链表的头节点 head ，请你反转链表，并返回反转后的链表。


# Definition for singly-linked list.
class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next
class Solution:
    def reverseList(self, head: [ListNode]) -> [ListNode]:
        temp_list = []
        while head != None:
            temp_list.append(head.val)
            head = head.next
        temp_list.append(head.val)
        length = len(temp_list)
        cc = [chr(97+i) for i in range(length)]
        dummy = ListNode(0)
        cur = dummy
        for i in range(length):
            cc[i] = ListNode(temp_list[length-i-1])
            cur.next = cc[i]
            cur = cur.next
        return dummy.next


#更新卡哥的双指针方法，有点懂了
class SolutionB:
    def reverseList(self, head: [ListNode]) -> [ListNode]:
        pre = None
        cur = head
        while cur != None:
            tmp = cur.next  # 1.保存下一个节点
            cur.next = pre  # 2.转方向
            pre = cur  # 3.pre右移
            cur = tmp  # 4. cur右移
        return pre


# 递归是抄的，还不是很明白
class SolutionC:
    def reverseList(self, head: [ListNode]) -> [ListNode]:
        def rever(pre,cur):
            if cur == None:
                return pre
            tmp = cur.next
            cur.next = pre
            return rever(cur,tmp)
        return rever(None,head)


if __name__ == '__main__':
    a = ListNode(1)
    b = ListNode(2)
    c = ListNode(3)
    d = ListNode(4)
    e = ListNode(5)
    a.next = b
    b.next = c
    c.next = d
    d.next = e
    ss = Solution()
    rr = ss.reverseList(a)
    #while a.next != None:
    #    print(a.val)
    #    a= a.next
    while rr.next != None:
        print(rr.val)
        rr = rr.next

# 虽然可能是有缺陷的解答，并不是最优解，但是这道题是我在没有看答案的情况下， 独自写出来的
# 有没有有一种可能，我理解了一点点，作为链表的含义，我知道了怎样去操作链表 ---2022-01-18
